package com.ruoyi.common.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import lombok.Data;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Data
public class AliyunSmsUtils {

    @Value("${sms.regionId}")
    private String regionId;

    @Value("${sms.accessKeyId}")
    private String accessKeyId;

    @Value("${sms.secret}")
    private String secret;

    @Value("${sms.signName}")
    private String signName;

    @Value("${sms.signNameOverseas}")
    private String signNameOverseas;

    @Value("${sms.goClassTemplateCode}")
    private String goClassTemplateCode;

    @Value("${sms.payTemplateCode}")
    private String payTemplateCode;

    @Value("${sms.leaveTemplateCode}")
    private String leaveTemplateCode;

    /**
     * 发送短信
     * @param templateCode 模板
     * @param phoneNumbers 手机号
     * @param param 参数
     */
    public SendSmsResponse sendMessage(String signName, String templateCode,String phoneNumbers,String param){
        //填入刚刚创建用户时记录的AccessKey ID和AccessKey Secret
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setSysRegionId(regionId);
        request.setPhoneNumbers(phoneNumbers);
        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        request.setTemplateParam(param);
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(response.getMessage());
            return response;
        }catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}
